<?php


namespace app\bonus\logic;


use app\bonus\model\WapCoupon;

class CouponLogic
{
    //优惠券 颜色
    public $coupon_color = [10=>'蓝色',20=>'红',30=>'紫',40=>'黄'];
    //是否为虚拟优惠券
    public $lq_type = [10=>'用户领取',20=>'抽奖领取'];
    //优惠券类型
    public $coupon_type = [10=>'满减券',20=>'折扣券'];
    //到期类型
    public $expire_type = [10=>'领取后生效',20=>'固定时间'];
    //适用范围
    public $apply_range = [10=>'全部商品',20=>'指定商品'];
    //承担类型
    public $bear_type = [1=>'商家承担',2=>'平台承担',3=>'比例承担'];

    /**
     * 验证
     * @param $data
     * @return array|bool
     * @throws \think\Exception
     */
    public function verifyFun($data){
        //返回值
        $rdata = array("code"=>0,"meg"=>"");
        //验证代理商名称
        if(empty($data['name'])){
            $rdata['code'] = 9999;
            $rdata['meg'] = "代理商名称不能为空";
            return $rdata;
        }

        //查询代理商名称是否存在
        $wdata['name'] = $data['name'];
        if(array_key_exists("coupon_id",$data)) $wdata['coupon_id'] = array("NEQ",$data['coupon_id']);
        $num = WapCoupon::where($wdata)->count();
        $wdata = null;
        if($num > 0){
            $rdata['code'] = 9998;
            $rdata['meg'] = "代理商名称已经存在";
            return $rdata;
        }

    }

    /**
     * 添加数据
     * @param $data
     * @return int|string
     */
    public function addData($data){
        //添加数据
        $adata = DealParamLogic::dealCouponParam($data);
        $adata['create_time'] = time();
        $adata['update_time'] = time();
        $res = WapCoupon::insertGetId($adata);
        return $res;
    }

    /**
     * 添加数据
     * @param $data
     * @return int|string
     */
    public function editData($data){
        //添加数据
        $adata = DealParamLogic::dealCouponParam($data);
        $adata['update_time'] = time();
        $res = WapCoupon::where(['coupon_id'=>$data['coupon_id']])->Update($adata);
        return $res;
    }

    /**
     * 查询列表数据
     * @param $wdata
     * @throws \think\exception\DbException
     */
    public function getDataList($wdata){
        $list = WapCoupon::getListData($wdata);
        if($list){
            $list = $list->each(function ($item,$key){
                //颜色
                $item['color_text'] = "";
                if(array_key_exists($item['color'],$this->coupon_color)){
                    $item['color_text'] = $this->coupon_color[(int)$item['color']];
                }
                $item['lq_type_text'] = "";
                if(array_key_exists($item['lq_type'],$this->lq_type)){
                    $item['lq_type_text'] = $this->lq_type[(int)$item['lq_type']];
                }
                $item['coupon_type_text'] = "";
                if(array_key_exists($item['coupon_type'],$this->coupon_type)){
                    $item['coupon_type_text'] = $this->coupon_type[(int)$item['coupon_type']];
                }
                $item['expire_type_text'] = "";
                if(array_key_exists($item['expire_type'],$this->expire_type)){
                    $item['expire_type_text'] = $this->expire_type[(int)$item['expire_type']];
                }
                $item['start_time_text'] = $item['start_time']? date("Y-m-d H:i:s",$item['start_time']):"";
                $item['end_time_text'] = $item['end_time'] ? date("Y-m-d H:i:s",$item['end_time']):"";
                $item['apply_range_text'] = "";
                if(array_key_exists($item['apply_range'],$this->apply_range)){
                    $item['apply_range_text'] = $this->apply_range[(int)$item['apply_range']];
                }
                $item['bear_type_text'] = "";
                if(array_key_exists($item['bear_type'],$this->bear_type)){
                    $item['bear_type_text'] = $this->bear_type[(int)$item['bear_type']];
                }
                $item['create_time_text'] = date("Y-m-d H:i:s",strtotime($item['create_time']));
                $item['update_time_text'] = date("Y-m-d H:i:s",strtotime($item['update_time']));
                return $item;
            });
        }
        return $list;

    }
}